package com.freeLearning.boot.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.freeLearning.boot.core.security.util.SecurityUtils;
import com.freeLearning.boot.system.enums.MessageEnum;
import com.freeLearning.boot.system.mapper.ChatMessageMapper;
import com.freeLearning.boot.system.mapper.UserMapper;
import com.freeLearning.boot.system.model.dto.ChatMessageDTO;
import com.freeLearning.boot.system.model.entity.ConsultantUserChatMessage;
import com.freeLearning.boot.system.model.entity.Message;
import com.freeLearning.boot.system.model.entity.User;
import com.freeLearning.boot.system.model.vo.MessageVO;
import com.freeLearning.boot.system.service.ConsultantUserChatMessageService;
import com.freeLearning.boot.system.service.MessageService;
import lombok.AllArgsConstructor;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

@Service
@AllArgsConstructor
public class ConsultantUserChatMessageServiceImpl extends ServiceImpl<ChatMessageMapper, ConsultantUserChatMessage> implements ConsultantUserChatMessageService {
    public final UserMapper userMapper;
    private final SimpMessagingTemplate messagingTemplate;
    private final MessageService messageService;

    @Override
    public boolean sendChatMessage(ChatMessageDTO chatMessageDTO) {
        StringBuilder content = new StringBuilder();
        if (chatMessageDTO.getIsConsultMessage()){
            content.append("用户:").append(chatMessageDTO.getSenderName()).append("给你发送了一条消息");

        }else{
            content.append("咨询师:").append(chatMessageDTO.getSenderName()).append("回复了你的咨询");

        }
        Message message = Message.builder()
                .title("收到一条新的消息")
                .content(content.toString())
                .type(MessageEnum.MESSAGE.getValue())
                .publisherId(chatMessageDTO.getSenderId())
                .publishTime(LocalDateTime.now())
                .createBy(chatMessageDTO.getSenderId())
                .acceptId(chatMessageDTO.getReceiverId())
                .build();
        messageService.sendMessage(message);
        // 接收人
        String receiver = userMapper.selectById(chatMessageDTO.getReceiverId()).getUsername();
        messagingTemplate.convertAndSendToUser(receiver, "/queue/chat/message", chatMessageDTO);
        // 未读消息数
        long count = this.count(
                new LambdaQueryWrapper<ConsultantUserChatMessage>()
                        .eq(ConsultantUserChatMessage::getReceiverId, chatMessageDTO.getReceiverId())
                        .eq(ConsultantUserChatMessage::getStatus, 0)
        );
        messagingTemplate.convertAndSendToUser(receiver, "/queue/chat/message/count", count+1L);
        return save(chatMessageDTO);
    }

    @Override
    public boolean readMessage(Long senderId) {
        Long userId = SecurityUtils.getUserId();
        String username = SecurityUtils.getUsername();
        User user = userMapper.selectById(senderId);
        boolean update = this.update(
                new LambdaUpdateWrapper<ConsultantUserChatMessage>()
                        .eq(ConsultantUserChatMessage::getSenderId, senderId)
                        .eq(ConsultantUserChatMessage::getReceiverId, userId)
                        .eq(ConsultantUserChatMessage::getStatus, 0)
                        .set(ConsultantUserChatMessage::getStatus, 1)
        );

        // 已读
        long count = this.count(
                new LambdaQueryWrapper<ConsultantUserChatMessage>()
                        .eq(ConsultantUserChatMessage::getReceiverId, userId)
                        .eq(ConsultantUserChatMessage::getStatus, 0)
        );
        messagingTemplate.convertAndSendToUser(username, "/queue/chat/message/count", count);
        messagingTemplate.convertAndSendToUser(user.getUsername(), "/queue/chat/message/read", userId);
        return update;
    }
}
